package com.huaxin.hxcloud.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import com.huaxin.hxcloud.R;

/**
 * @author lwjing
 * @date 2018/4/12
 * @Desc $desc$
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class TaskFeeDialog extends Dialog {

    private String[] mSelectArray = {"结束任务", "结束任务并还车", "取消"};
    private ItemClickCallBack callBack;
    private ListView          lv;
    private EditText          et_task_fee;
    private boolean           hasFee;

    public TaskFeeDialog(@NonNull Context context) {
        this(context,R.style.DialogTheme);
    }

    public TaskFeeDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.DialogTheme);
    }

    protected TaskFeeDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popupwindow_select_list);
        initView();
    }

    private void initView() {
        lv = (ListView) findViewById(R.id.lv);
        et_task_fee = (EditText) findViewById(R.id.et_task_fee);
        et_task_fee.setVisibility(View.GONE);
        ArrayAdapter popupAdapter = new ArrayAdapter(getContext(), R.layout.simple_list_item, mSelectArray);
        lv.setAdapter(popupAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (position == adapterView.getCount() - 1) {
                    dismiss();
                } else {
                    String fee=et_task_fee.getText().toString().trim();
//                    if (hasFee) {
//                        if (TextUtils.isEmpty(fee)) {
//                            ToastUtil.showToast("请填写实际产生费用");
//                            return;
//                        }
//                    }
                    if (callBack != null) {
                        callBack.itemClick(position, fee);
                    }
                    dismiss();
                }
            }
        });
    }

    public void setEtFeeVisible(boolean isVisible) {
        this.hasFee = isVisible;
        if (isVisible) {
            et_task_fee.setVisibility(View.VISIBLE);
        } else {
            et_task_fee.setVisibility(View.GONE);
        }

    }

    public void setItemClickCallBack(ItemClickCallBack callBack) {
        this.callBack = callBack;
    }

    public interface ItemClickCallBack {
        void itemClick(int position, String fee);
    }
}
